/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.entity.entity.dynamicProp.DynamicPropSetImpl;
import com.inspur.edp.cef.entity.entity.dynamicProp.IDynamicPropSet;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NestedAccessorFieldGenerator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class DynamicPropSetPropertyGenerator extends ClassPropertyGenerator {

  protected MediatePropertyInfo pi;

  public DynamicPropSetPropertyGenerator(MediatePropertyInfo pi) {
    this.pi = pi;
  }

  public static String getFieldName(MediatePropertyInfo propName) {
    return getFieldName((IGspCommonField) propName.getElement());
  }

  public static String getFieldName(IGspCommonField field) {
    return NestedAccessorFieldGenerator.getFieldName(field);
  }

  //region Impl
  @Override
  protected String getFieldName() {
    return getFieldName(pi);
  }


  @Override
  protected final String getPropertyName() {
    return pi.getPropertyName();
  }


  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
    return getGetterAccessModifier();
  }

  @Override
  protected Block buildGetMethodBody() {
    Block block = ast.newBlock();

    IfStatement ifStatement = ast.newIfStatement();
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setLeftOperand(ast.newSimpleName(getFieldName()));
    infixExpression.setRightOperand(ast.newNullLiteral());
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    ifStatement.setExpression(infixExpression);
    Block ifThenBlock = ast.newBlock();
    Assignment assignment = ast.newAssignment();
    assignment.setLeftHandSide(ast.newSimpleName(getFieldName()));
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(DynamicPropSetImpl.class
        .getTypeName())));
    assignment.setRightHandSide(classInstanceCreation);
    ifThenBlock.statements().add(ast.newExpressionStatement(assignment));
    ifStatement.setThenStatement(ifThenBlock);
    block.statements().add(ifStatement);

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName(getFieldName()));
    block.statements().add(returnStatement);

    return block;
  }

  @Override
  protected Block buildSetMethodBody() {
    Assignment assignment = ast.newAssignment();
    assignment.setLeftHandSide(ast.newSimpleName(getFieldName()));
    assignment.setRightHandSide(ast.newSimpleName("value"));
    Block block = ast.newBlock();
    block.statements().add(ast.newExpressionStatement(assignment));
    return block;
  }


  @Override
  protected TypeInfo getPropertyType() {
    return new TypeInfo(IDynamicPropSet.class);
  }
  //endregion
}
